using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VVTBEW
    {
        public DataRow Convert2VTBEW(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryVTBEW(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["VTBWMANDANT"] = " ".PadRight(2 ); 
         dwReturn["VTBWJJJJ"] = " ".PadRight(0 ); 
         dwReturn["VTBWBELEG"] = " ".PadRight(0 ); 
         dwReturn["VTBWLFDNR"] = " ".PadRight(0 ); 
         dwReturn["VTBWTPMA"] = " ".PadRight(4 ); 
         dwReturn["VTBWKUNDNRE"] = " ".PadRight(8 ); 
         dwReturn["VTBWKUNDNRB"] = " ".PadRight(8 ); 
         dwReturn["VTBWTPMNR"] = " ".PadRight(6 ); 
         dwReturn["VTBWANZ"] = " ".PadRight(0 ); 
         dwReturn["VTBWLIEFSCH"] = " ".PadRight(10 ); 
         dwReturn["VTBWBS"] = " ".PadRight(2 ); 
         dwReturn["VTBWBUCHDAT"] = " ".PadRight(0 ); 
         dwReturn["VTBWBUCHUHR"] = " ".PadRight(0 ); 
         dwReturn["VTBWBUCHBEN"] = " ".PadRight(8 ); 
         dwReturn["VTBWWERTDAT"] = " ".PadRight(0 ); 
         dwReturn["VTBWFILLER"] = " ".PadRight(28 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapVTBEW(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveVTBEW(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryVTBEW(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapVTBEW(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertVTBEW(dwLocal, ITXDBVERSION);
            }
            UpdateVTBEW(dwLocal);
        }
        public void InsertVTBEW(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO VTBEW(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryVTBEW(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM VTBEW WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM VTBEW  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "VTBEW";
            Return = Result;
            return Return;
        }
        public void DeleteVTBEW(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM VTBEW   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateVTBEW(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE VTBEW SET "
                    +"VTBWMANDANT= :VTBWMANDANT,"
                    +"VTBWJJJJ= :VTBWJJJJ,"
                    +"VTBWBELEG= :VTBWBELEG,"
                    +"VTBWLFDNR= :VTBWLFDNR,"
                    +"VTBWTPMA= :VTBWTPMA,"
                    +"VTBWKUNDNRE= :VTBWKUNDNRE,"
                    +"VTBWKUNDNRB= :VTBWKUNDNRB,"
                    +"VTBWTPMNR= :VTBWTPMNR,"
                    +"VTBWANZ= :VTBWANZ,"
                    +"VTBWLIEFSCH= :VTBWLIEFSCH,"
                    +"VTBWBS= :VTBWBS,"
                    +"VTBWBUCHDAT= :VTBWBUCHDAT,"
                    +"VTBWBUCHUHR= :VTBWBUCHUHR,"
                    +"VTBWBUCHBEN= :VTBWBUCHBEN,"
                    +"VTBWWERTDAT= :VTBWWERTDAT,"
                    +"VTBWFILLER= :VTBWFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("VTBWMANDANT", OracleType.NVarChar).Value = dwData["VTBWMANDANT"].ToString(); 
          or1.Parameters.Add("VTBWJJJJ", OracleType.NVarChar).Value = dwData["VTBWJJJJ"].ToString(); 
          or1.Parameters.Add("VTBWBELEG", OracleType.NVarChar).Value = dwData["VTBWBELEG"].ToString(); 
          or1.Parameters.Add("VTBWLFDNR", OracleType.NVarChar).Value = dwData["VTBWLFDNR"].ToString(); 
          or1.Parameters.Add("VTBWTPMA", OracleType.NVarChar).Value = dwData["VTBWTPMA"].ToString(); 
          or1.Parameters.Add("VTBWKUNDNRE", OracleType.NVarChar).Value = dwData["VTBWKUNDNRE"].ToString(); 
          or1.Parameters.Add("VTBWKUNDNRB", OracleType.NVarChar).Value = dwData["VTBWKUNDNRB"].ToString(); 
          or1.Parameters.Add("VTBWTPMNR", OracleType.NVarChar).Value = dwData["VTBWTPMNR"].ToString(); 
          or1.Parameters.Add("VTBWANZ", OracleType.NVarChar).Value = dwData["VTBWANZ"].ToString(); 
          or1.Parameters.Add("VTBWLIEFSCH", OracleType.NVarChar).Value = dwData["VTBWLIEFSCH"].ToString(); 
          or1.Parameters.Add("VTBWBS", OracleType.NVarChar).Value = dwData["VTBWBS"].ToString(); 
          or1.Parameters.Add("VTBWBUCHDAT", OracleType.NVarChar).Value = dwData["VTBWBUCHDAT"].ToString(); 
          or1.Parameters.Add("VTBWBUCHUHR", OracleType.NVarChar).Value = dwData["VTBWBUCHUHR"].ToString(); 
          or1.Parameters.Add("VTBWBUCHBEN", OracleType.NVarChar).Value = dwData["VTBWBUCHBEN"].ToString(); 
          or1.Parameters.Add("VTBWWERTDAT", OracleType.NVarChar).Value = dwData["VTBWWERTDAT"].ToString(); 
          or1.Parameters.Add("VTBWFILLER", OracleType.NVarChar).Value = dwData["VTBWFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
